package net.sycu.guomy.ems.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sycu.guomy.ems.entity.*;
import net.sycu.guomy.ems.business.*;
import net.sycu.guomy.ems.common.MyConst;

public class HeaderServlet extends BaseServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Timestamp lastLoginDateTime = MyConst.TIMESTAMPMIN;
		LoginBean login = this.getLogin();
		if (login != null) {
			LoginLogSearcher loginLogSearcher = new LoginLogSearcher();
			loginLogSearcher.setLoginSearcher(new LoginSearcher());
			loginLogSearcher.setLoginId(login.getLoginId());
			
			LoginLogService loginLogService = new LoginLogService();
			List<LoginLogBean> loginLogs = loginLogService.search(loginLogSearcher, "LoginDateTime DESC");
			if (loginLogs != null && loginLogs.size() > 0)
				lastLoginDateTime = loginLogs.get(0).getLoginDateTime();
	
			MessageService messageService = new MessageService();
			MessageSearcher messageSearcher = new MessageSearcher();
			messageSearcher.setDepartmentSearcher(new DepartmentSearcher());
			messageSearcher.setPublisherSearcher(new LoginSearcher());
			
			messageSearcher.setPublishDateTimeBegin(lastLoginDateTime);
			List<MessageBean> publicMessages = messageService.searchPublicMessages(messageSearcher, "PublishDateTime DESC");
			
			messageSearcher.setDepartmentId(login.getDepartmentId());
			List<MessageBean> departmentMessages = messageService.searchDepartmentMessages(messageSearcher, "PublishDateTime DESC");
	
			List<NoteBean> notes = new NoteService().searchSendeeNoRead(login.getLoginId());
			request.setAttribute("headPublicMessages", publicMessages);
			request.setAttribute("headPublicMessagesSize", publicMessages.size());
			request.setAttribute("headDepartmentMessages", departmentMessages);
			request.setAttribute("headDepartmentMessagesSize", departmentMessages.size());
			request.setAttribute("headNotes", notes);
			request.setAttribute("headNotesSize", notes.size());
		}
		request.getRequestDispatcher("/header.jsp").include(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
